/**
 * @function 标签模块api
 * @author anytao
 * @created 2016-07-22
 * @updated 2016-08-09
 * @version 1.0
 */
'use strict';
import Base from './base.js';

export default class extends Base {

    /**
     * @description 标签列表页面获取数据
     * @param
     * @return
     */
    async listAction() {

        let search = this.post();
        let query = {};
        let order = {};

        let tags = await this.model('tag')
            .order(order)
            .where(query)
            .page(this.post('pageCurrent'), this.post('pageSize' || 15))
            .countSelect();

        tags.pageCurrent = tags.currentPage;
        tags.total = tags.count;
        tags.list = tags.data;
        delete tags.data;

        return this.json(tags);
    }

    /**
     * @description 标签新增/编辑
     * @param
     * @return
     */
    async editAction() {

        let result = {
            "statusCode": "200",
            "message": "新增成功",
            "tabid": "table, table-fixed",
            "closeCurrent": true,
            "forward": "",
            "forwardConfirm": "",
            "id": 0,
            'type': 'update' //新增or编辑
        };

        let tagId = 0;
        let tag = this.post();

        if (tag.id == 0) {
            result.type = 'add';
            tag.id = null;
            tag.created_at = think.datetime();
            tagId = await this.model('tag').add(tag);
            result.id = tagId;
        } else {
            result.message = "编辑成功";
            tag.updated_at = think.datetime();
            tagId = await this.model('tag').where({
                id: tag.id
            }).update(tag);
            result.id = tag.id;
        }
        return this.json(result);
    }

    /**
     * 标签删除接口
     */
    async deleteAction() {

        let tagIds = this.post('tagIds');
        let result = {
            statusCode: '200',
            message: '删除成功',
            tagIds: tagIds
        };

        let deleteMsg = await this.model('tag').where({
            id: ["IN", tagIds]
        }).delete();
        return this.json(result);
    }

    /**
     * 获取单条记录
     * 编辑页面调用
     */
    async getAction() {

        let tag = {
            id: 0
        };
        let tagId = this.get('tagId');

        if (tagId) {
            tag = await this.model('tag').where({
                id: tagId
            }).find();
        }

        return this.json(tag);
    }
}